package com.test.resumeanalysis.ui.home.post_info

import android.view.View
import android.widget.AdapterView
import android.widget.ArrayAdapter
import android.widget.Spinner
import android.widget.Toast
import androidx.fragment.app.activityViewModels
import androidx.lifecycle.Observer
import androidx.navigation.fragment.findNavController
import androidx.recyclerview.widget.LinearLayoutManager
import com.setruth.mvvmbaseproject.util.LogUtil
import com.test.resumeanalysis.App.App
import com.test.resumeanalysis.R
import com.test.resumeanalysis.common.BaseVBFragment
import com.test.resumeanalysis.data.model.JobInfo
import com.test.resumeanalysis.databinding.FragmentPostInfoBinding
import com.test.resumeanalysis.viewModel.PublicViewModel


class PostInfoFragment : BaseVBFragment<FragmentPostInfoBinding>(FragmentPostInfoBinding::inflate){

    //注意这里是 activityViewModels 就代表这个viewmodel是整个activity的所有组件共享
    private val model: PublicViewModel by activityViewModels()

    private lateinit var adapter:TalentAdapter

    override fun initView() {
        super.initView()


        setupToolbar(binding.postInfoToolbar,true)

        binding.postInfoToolbar.setNavigationOnClickListener{
            findNavController().popBackStack()
        }

        model.selectedPost.observe(viewLifecycleOwner, Observer<JobInfo> {
            binding.tvTitle.text = it.jobname
            binding.tvDutyInfo.text  = it.responsibility
//            binding.tvWages.text = it.wages
            binding.tvRequireInfo.text = it.requirement
        });

        val linerLayout = LinearLayoutManager(activity)
        adapter = TalentAdapter()
        val list = ArrayList<String>()
        list.let {
            it.add("a")
            it.add("b")
            it.add("c")
            it.add("d")
            it.add("b")
            it.add("c")
            it.add("c")
        }
        adapter.data = list

        binding.rvPostInfo.layoutManager = linerLayout
        binding.rvPostInfo.adapter = adapter

    }


    private fun initSpinner() {
        //声明一个下拉列表的数组适配器
//        val starAdapter: ArrayAdapter<String> =
//            ArrayAdapter<String>(this, R.layout.item_select, starArray)
//        //设置数组适配器的布局样式
//        starAdapter.setDropDownViewResource(R.layout.item_dropdown)
        //从布局文件中获取名叫sp_dialog的下拉框
        val sp: Spinner =binding.spinnerSchool
        //设置下拉框的标题，不设置就没有难看的标题了
        sp.setPrompt("请选择配送方式")
        //设置下拉框的数组适配器
//        sp.setAdapter(starAdapter)
        //设置下拉框默认的显示第一项
        sp.setSelection(0)
        //给下拉框设置选择监听器，一旦用户选中某一项，就触发监听器的onItemSelected方法
        sp.setOnItemSelectedListener(object :AdapterView.OnItemSelectedListener{
            override fun onItemSelected(p0: AdapterView<*>?, p1: View?, p2: Int, p3: Long) {
                Toast.makeText(App.appContext, "您选择的是：" + p2, Toast.LENGTH_SHORT)
                    .show()
                LogUtil.e("aaaaaa")
            }

            override fun onNothingSelected(p0: AdapterView<*>?) {
                TODO("Not yet implemented")
            }
        })
    }

    internal class MySelectedListener : AdapterView.OnItemSelectedListener {
        override fun onItemSelected(adapterView: AdapterView<*>?, view: View?, i: Int, l: Long) {
            Toast.makeText(App.appContext, "您选择的是：" + i, Toast.LENGTH_SHORT)
                .show()
        }

        override fun onNothingSelected(adapterView: AdapterView<*>?) {}
    }
}